package user

import (
	"context"
	"strconv"
	v1 "video-api/api/v1"
	"video-api/internal/middleware"
	"video-api/internal/service"
	"video-api/utility/metrics"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gtime"
)

type UserController struct{}

var UserHandle = new(UserController)

func (u *UserController) Login(ctx context.Context, req *v1.LoginReq) (*v1.LoginRes, error) {
	g.Log().Info(ctx, "UserController-Login param ", g.Map{"req": req})
	user, err := service.UserService().LoginCallback(ctx, req)
	if err != nil {
		g.Log().Info(ctx, "UserController-Login err ", err)
		return nil, err
	}
	// 更新登录次数指标
	metrics.UserLoginAttempts.WithLabelValues(strconv.Itoa(int(user.UserId))).Inc()
	return user, nil
}

func (u *UserController) GetUserInfo(ctx context.Context, param *v1.UserInfoReq) (*v1.UserInfoRes, error) {
	g.Log().Info(ctx, "UserController-GetUserInfo param ", g.Map{"req": param})
	userInfoLogin := middleware.GetUserInfo(ctx)
	if userInfoLogin == nil || userInfoLogin.UserId == 0 {
		return nil, gerror.New("user not exist")
	}
	param.UserId = userInfoLogin.UserId
	userInfo, err := service.UserService().GetUserInfoById(ctx, param)
	if err != nil {
		g.Log().Error(ctx, "UserController-GetUserInfoById err: ", err.Error(), g.Map{"userId": param.UserId})
		return nil, err
	}
	res := &v1.UserInfoRes{
		UserId:    userInfo.Id,
		UserCode:  userInfo.UserCode,
		UserName:  userInfo.NickName,
		Email:     userInfo.Email,
		AvatarURL: userInfo.Avatar,
		Coins:     userInfo.Coins,
		Bonus:     userInfo.Bonus,
		IsLogin:   userInfo.IsLogin,
		LoginType: userInfo.LoginType,
	}
	// 判断用户vip是否有效
	if userInfo.VipEndTime != nil && !userInfo.VipEndTime.IsZero() {
		if userInfo.VipEndTime.After(gtime.Now()) {
			res.VipValid = true
		}
	}
	return res, nil
}

// LoginOut 登出
func (u *UserController) LoginOut(ctx context.Context, req *v1.LoginOutReq) (*v1.UserInfoRes, error) {
	//info := ctx.Value("userInfo").(*middleware.ClaimsInfo)
	//if info == nil {
	//	return nil, errors.New("用户信息获取失败")
	//}
	//err := redis_client.RedisClient.Del(context.TODO(), middleware.LOGIN_TOKEN+info.UserId).Err()
	//if err != nil {
	//	g.Log().Error(context.TODO(), "userService LoginOut DelResult is err, ", err)
	//	return nil, err
	//}
	g.Log().Info(ctx, "UserController-LoginOut param ", g.Map{"req": req})
	_, err := service.UserService().LoginOut(ctx)
	if err != nil {
		g.Log().Info(ctx, "UserController-LoginOut err ", err)
		return nil, err
	}
	return nil, nil
}

func (u *UserController) CreateUser(ctx context.Context, req *v1.CreateUserReq) (*v1.LoginRes, error) {
	g.Log().Info(ctx, "UserController-CreateUser param ", g.Map{"req": req})
	user, err := service.UserService().CreateUser(ctx, req)
	if err != nil {
		g.Log().Info(ctx, "UserController-CreateUser err ", err)
		return nil, err
	}
	// 更新登录次数指标
	metrics.UserLoginAttempts.WithLabelValues(strconv.Itoa(int(user.UserId))).Inc()
	return user, nil
}

// SetAutoPlay 设置用户自动播放
func (u *UserController) SetAutoPlay(ctx context.Context, req *v1.AutoPlayReq) (*v1.UserInfoRes, error) {
	g.Log().Info(ctx, "UserController-SetAutoPlay param ", g.Map{"req": req})
	userInfoLogin := middleware.GetUserInfo(ctx)
	if userInfoLogin == nil || userInfoLogin.UserId == 0 {
		return nil, gerror.New("AutoPlay user not exist")
	}
	req.UserId = userInfoLogin.UserId
	err := service.UserService().AutoPlay(ctx, req)
	if err != nil {
		g.Log().Info(ctx, "UserController-SetAutoPlay err ", err)
		return nil, err
	}
	return u.GetUserInfo(ctx, &v1.UserInfoReq{UserId: userInfoLogin.UserId})
}
